package com.app.core.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.android.tu.loadingdialog.LoadingDailog;
import com.app.core.config.MyApp;
import com.app.core.presenter.ParseData;
import com.app.core.presenter.ParseDataImpl;
import com.app.core.util.LogUtils;
import com.app.core.util.Sputil;
import com.app.core.util.ViewFinder;
import com.app.core.util.ViewUtils;

public abstract class BaseNewFragment extends FixedOnActivityResultBugFragment {
    private static final String TAG = "BaseFragment";
    //上下文
    protected Context mContext;
    //绑定这个activity布局的Finder
    protected ViewFinder finder;
    //intent
    protected Intent intent;
    //SharedPreferences工具类
    protected Sputil sp;
    //加载框
    protected LoadingDailog.Builder loadBuilder;
    protected LoadingDailog dialog;
    //网络请求
    protected ParseData parseData;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        LogUtils.i(getFragmentName(), "onAttach()");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = MyApp.getInstance();
        LogUtils.i(getFragmentName(), "onCreate()");
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        finder = new ViewFinder(view);
        intent = new Intent();
        sp = MyApp.getInstance().getSp();
        parseData=new ParseDataImpl();
        LogUtils.i(getFragmentName(), "onViewCreated()");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LogUtils.i(getFragmentName(), "onActivityCreated()");
    }

    @Override
    public void onStart() {
        super.onStart();
        LogUtils.i(getFragmentName(), "onStart()");
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtils.i(getFragmentName(), "onResume()");
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtils.i(getFragmentName(), "onPause()");
    }

    @Override
    public void onStop() {
        super.onStop();
        LogUtils.i(getFragmentName(), "onStop()");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LogUtils.i(getFragmentName(), "onDestroyView()");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.i(getFragmentName(), "onDestroy()");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        LogUtils.i(getFragmentName(), "onDetach()");
    }

    /**
     * fragment name
     */
    protected abstract String getFragmentName();

    /**
     * 展示View
     *
     * @param view
     * @return
     */
    protected BaseNewFragment show(final View view) {
        ViewUtils.setGone(view, false);
        return this;
    }

    /**
     * 隐藏View
     *
     * @param view
     * @return
     */
    protected BaseNewFragment hide(final View view) {
        ViewUtils.setGone(view, true);
        return this;
    }

    /**
     * 显示加载框
     *
     * @param text
     */
    public void showDialog(String text) {
        loadBuilder = new LoadingDailog.Builder(getActivity())
                .setMessage(text)
                .setCancelable(true)
                .setCancelOutside(false);
        dialog = loadBuilder.create();
        dialog.show();


    }

    /**
     * 关闭加载框
     */
    public void closeDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }


}
